package com.pagoda.nerp.trade.app.quality.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pagoda.nerp.trade.common.BaseResult;
import com.pagoda.nerp.trade.common.enums.ErrorCodeMappingEnums;
import com.pagoda.nerp.trade.common.exception.NORecordInDBException;
import com.pagoda.nerp.trade.common.exception.QualityException;
import com.pagoda.nerp.trade.entity.MdQualityRecord;
import com.pagoda.nerp.trade.service.QualityService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/quality")
public class QualityController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private QualityService qualityService;

    /**
     * 申请质检
     * @param map
     * @return
     */
    @RequestMapping(value = "applyQualityTest", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Object> applyQualityTest(@RequestBody Map map) {
        logger.debug("applyQualityTest ===>> Enter method applyQualityTest");

        try {
            //参数校验
            JSONObject jsonObject = (JSONObject) JSON.toJSON(map.get("qualityRecord"));
            MdQualityRecord qualityRecord = JSON.toJavaObject(jsonObject, MdQualityRecord.class);
            if (qualityRecord == null || qualityRecord.getVendorId() == null || qualityRecord.getGoodsId() == null) {
                logger.debug("applyQualityTest ===>> Invalid input parameter,mdQualityRecord=null");
                return new BaseResult<>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS);
            }

            //存储进数据库
            int i = qualityService.applyQualityTest(qualityRecord, map.get("pics"));
            if (i < 0) {
                return new BaseResult<>(ErrorCodeMappingEnums.ERROR_EXCEPTION_DB_DML);
            }
        } catch (NORecordInDBException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (QualityException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (Exception e) {
            logger.error("applyQualityTest ===>> 系统异常", e);
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR,e.getMessage());
        }

        return new BaseResult<>(ErrorCodeMappingEnums.SUCCESS, ErrorCodeMappingEnums.SUCCESS.getErrorMsg());
    }

    /**
     * 查询质检记录明细
     * @param map
     * @return
     */
    @RequestMapping(value = "queryDetailQualityRecord", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<MdQualityRecord> queryDetailQualityRecord(@RequestBody Map<String, String> map) {
        logger.debug("queryDetailQualityRecord ===>> Enter method queryQualityRecord");

        MdQualityRecord qualityRecord;
        try {
            //参数校验
            int qualityRecordId = Integer.valueOf(map.get("qualityRecordId"));
            if (qualityRecordId < 0) {
                logger.debug("queryDetailQualityRecord ===>> Invalid input parameter,qualityRecordId=" + qualityRecordId);
                return new BaseResult<>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS);
            }

            qualityRecord = qualityService.queryQualityRecordByID(qualityRecordId);
            if (qualityRecord == null) {
                logger.debug("queryDetailQualityRecord ===>> No MdQualityRecord in database,qualityRecordId=" + qualityRecordId);
                return new BaseResult<>(ErrorCodeMappingEnums.ERROR_INVALID_DB_RECORD);
            }
        } catch (NORecordInDBException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (QualityException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (Exception e) {
            logger.error("queryDetailQualityRecord ===>> 系统异常", e);
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR,e.getMessage());
        }

        return new BaseResult<>(ErrorCodeMappingEnums.SUCCESS, qualityRecord);
    }


    /**
     * 查询供应商质检记录列表
     *
     * @param map{vendorId,
     * @return
     */
    @RequestMapping(value = "queryQualityRecordList", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<List<MdQualityRecord>> queryQualityRecordList(@RequestBody Map<String, String> map) {

        logger.debug("queryQualityRecordList ===>> Enter method queryQualityRecordList");

        //参数校验
        if (map.get("vendorId") == null && map.get("goodsId") == null) {
            logger.debug("queryQualityRecordList ===>> Invalid input parameter");
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS);
        }

        List<MdQualityRecord> qualityRecordList;
        try {

            qualityRecordList = qualityService.queryQualityRecordList(map);
            if (qualityRecordList == null || qualityRecordList.size() == 0) {
                logger.debug("queryQualityRecordList ===>> No MdQualityRecord in database,vendorId=" + map.get("vendorId") + " ,goodsId:" + map.get("goodsId"));
                return new BaseResult<>(ErrorCodeMappingEnums.ERROR_INVALID_DB_RECORD);
            }

        } catch (NORecordInDBException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (QualityException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (Exception e) {
            logger.error("系统异常", e);
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR,e.getMessage());
        }
        return new BaseResult<>(ErrorCodeMappingEnums.SUCCESS, qualityRecordList);
    }

    /**
     * 添加快递信息
     * @param map
     * @return
     */
    @RequestMapping(value = "addExpressInfo", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Object> addExpressInfo(@RequestBody Map<String, String> map) {

        logger.debug("addExpressInfo ===>> Enter method updateQualityRecord");
        //参数校验
        if (map == null || StringUtils.isEmpty(map.get("qualityRecordId")) || StringUtils.isEmpty(map.get("qualityPlatId")) ||
                StringUtils.isEmpty(map.get("expressCompany")) || StringUtils.isEmpty(map.get("costBelong")) || StringUtils.isEmpty(map.get("expressNumber"))) {
            logger.debug("addExpressInfo ===>> Invalid input parameter");
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS);
        }

        try {
            MdQualityRecord qualityRecord = new MdQualityRecord();
            qualityRecord.setQualityRecordId(Integer.valueOf(map.get("qualityRecordId")));
            qualityRecord.setExpressCompany(map.get("expressCompany"));
            qualityRecord.setExpressNumber(map.get("expressNumber"));
            qualityRecord.setCostBelong(map.get("costBelong"));
            qualityRecord.setQualityPlatId(map.get("qualityPlatId"));

            //存储进数据库
            int i = qualityService.updateQualityRecord(qualityRecord);
            if (i < 0) {
                return new BaseResult<>(ErrorCodeMappingEnums.ERROR_EXCEPTION_DB_DML);
            }

        } catch (NORecordInDBException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (QualityException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (Exception e) {
            logger.error("addExpressInfo ===>> 系统异常", e);
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR,e.getMessage());
        }

        return new BaseResult<>(ErrorCodeMappingEnums.SUCCESS);
    }

    @RequestMapping(value = "queryQualityApplyResult", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Object> queryQualityApplyResult(@RequestBody Map<String, String> map) {
        logger.debug("Enter method queryQualityApplyResult");
        if (StringUtils.isEmpty(map.get("qualityPlatId")) ||StringUtils.isEmpty( map.get("vendorId")) ||StringUtils.isEmpty( map.get("qualityRecordId"))) {
            logger.debug("queryQualityApplyResult ===>> Invalid input parameter");
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR_INVALID_PARAMS);
        }

        try {
            qualityService.queryQualityApplyResult(map);

        } catch (NORecordInDBException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (QualityException e) {
            return new BaseResult<>(e.getErrorCodeMappingEnums());
        } catch (Exception e) {
            logger.error("queryQualityApplyResult ===>> 系统异常", e);
            return new BaseResult<>(ErrorCodeMappingEnums.ERROR,e.getMessage());
        }

        return new BaseResult<>(ErrorCodeMappingEnums.SUCCESS);
    }



}
